
from djapplication import DjApplication
from Tkinter import Toplevel
from djwindow import DjWindow

class DjApplicationSubwindow(DjApplication):
	def __init__(self):
		windowBaseControl = Toplevel()
		self._setup(windowBaseControl)
		self.window.basecontrol.withdraw() #don't show it yet.
		
	def _setup(self, windowBaseControl):
		self._tkroot = windowBaseControl._root()
		self.window = DjWindow(windowBaseControl)
		self.layout(self.window)
		
		menubar = self.menubar(self.window)
		if menubar:
			self.window.menubar = menubar
		
	def run_dialog(self):
		"""Display dialog as modal. Cannot return until the quit() method is called. Then closes the dialog. Typically, a button in the dialog will set some type of result flag on the window (self._result=2), which can be retrieved later, and call self.quit()."""
		self.window.basecontrol.deiconify()
		
		self.window.basecontrol.update_idletasks()
		self.window.basecontrol.wait_visibility()
		self.window.basecontrol.grab_set()
		self.window.basecontrol.update_idletasks()
		self.window.basecontrol.mainloop()
		self.window.basecontrol.destroy()
		
		#~ self.window.basecontrol.withdraw()
	
	def end_dialog(self):
		'''ends dialog'''
		"""Stops the current event loop. Use in a dialog after run_dialog has been called."""
		
		self.window.basecontrol.quit()
		

		
		
	def close(self):
		"""Closes the current window and removes it."""
		#This does not stop the current event loop, so use quit(),close() and not close() to close a dialog
		self.window.basecontrol.destroy()
		